"""我方飞机"""
import pygame
from pygame.sprite import Sprite
import constants


class MyPlane(Sprite):
    """我方飞机类"""

    # 飞机移动的偏移量
    offset = 10
    # 左右偏移量（飞机左右可移出窗口的偏移量）
    horizontal_offset = 40
    # 自身动画（尾部动画）播放频率计数器
    animation_counter = 0

    def __init__(self, window):
        super().__init__()
        # 创建一个透明的1像素点
        self.image_none = pygame.Surface((1, 1), pygame.SRCALPHA)
        self.image1 = pygame.image.load("images/my_plane1.png")
        self.image2 = pygame.image.load("images/my_plane2.png")
        self.image = self.image1
        self.window = window
        # 获取我方飞机矩形
        self.rect = self.image.get_rect()
        # 获取窗口矩形
        self.window_rect = self.window.get_rect()
        # 设置我方飞机底部居中
        self.rect.midbottom = self.window_rect.midbottom
        # 标记我方飞机在不同方向的移动状态
        self.is_move_up = False
        self.is_move_down = False
        self.is_move_Left = False
        self.is_move_Right = False
        # 我方飞机初始生命值
        self.life = constants.INITIAL_LIFE_OF_MY_PLANE
        # 飞机的生命值图片
        self.image_life = pygame.transform.scale(self.image1, (self.rect.width / 2, self.rect.height / 2))
        # 飞机的生命值图片矩形列表
        self.life_rect_list = []
        for i in range(0, self.life):
            life_rect = self.image_life.get_rect()
            # 计算每个生命值图片的位置
            life_rect.bottom = self.window_rect.bottom - constants.MARGIN_OF_PLANE_LIFE_IMAGE
            life_rect.right = (self.window_rect.right - constants.MARGIN_OF_PLANE_LIFE_IMAGE
                               - (self.image_life.get_rect().width + constants.MARGIN_OF_PLANE_LIFE_IMAGE) * i)
            # 将每个生命值飞机图片的矩形放入到list中
            self.life_rect_list.append(life_rect)
        # 标记我方飞机是否是无敌状态
        self.is_invincible = False

    def draw(self):
        """画出飞机"""
        self.window.blit(self.image, self.rect)

    def update(self):
        """更新我方飞机"""
        if self.is_move_up and self.rect.top > 0:  # 向上移动
            self.rect.top -= MyPlane.offset
        if self.is_move_down and self.rect.bottom < self.window_rect.bottom:  # 向下移动
            self.rect.bottom += MyPlane.offset
        if self.is_move_Left and self.rect.left + MyPlane.horizontal_offset > 0:  # 向左移动
            self.rect.left -= MyPlane.offset
        if self.is_move_Right and self.rect.right - MyPlane.horizontal_offset < self.window_rect.right:  # 向右移动
            self.rect.right += MyPlane.offset

    def start_animation(self):
        """开始播放动画（切换图片）"""
        self.animation_counter += 1
        if self.animation_counter == constants.ANIM_FREQUENCY_OF_MY_PLANE:
            # 无敌时让飞机闪烁
            if self.is_invincible:
                if self.image == self.image_none:
                    self.image = self.image1
                elif self.image == self.image1:
                    self.image = self.image2
                elif self.image == self.image2:
                    self.image = self.image_none
            else:  # 正常播放飞机尾部动画
                if self.image == self.image_none:
                    self.image = self.image1
                elif self.image == self.image1:
                    self.image = self.image2
                elif self.image == self.image2:
                    self.image = self.image1
            self.animation_counter = 0

    def reset_position(self):
        """重置飞机位置"""
        self.rect.midbottom = self.window_rect.midbottom


